<template>
  <div class="app-container">

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col>-->
<!--        <el-button type="primary" plain @click="addAccommodation">添加酒店</el-button>-->
<!--      </el-col>-->
<!--    </el-row>-->

    <el-form ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="项目名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="酒店名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--    表格数据-->
    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" fixed="left" :selectable="selectable" />-->
      <el-table-column label="酒店名称" prop="name" min-width="100"/>
      <el-table-column label="酒店地址" prop="address" min-width="100"/>
      <el-table-column label="房型" prop="houseType" min-width="80"/>
      <el-table-column label="价格（元）" prop="spend" min-width="50"/>
      <el-table-column label="入住时间" prop="moveIntoTimeStr" min-width="90"/>
      <el-table-column label="离店时间" prop="leaveTimeStr" min-width="90"/>
      <el-table-column label="餐食" prop="haveDinner" min-width="30">
        <template #default="scope">
          <el-space v-if="scope.row.haveDinner == 0 " type="text">否</el-space>
          <el-space v-if="scope.row.haveDinner == 1 " type="text">是</el-space>
        </template>
      </el-table-column>
<!--      <el-table-column label="房间数量" prop="houseCount" min-width="50"/>-->
<!--      <el-table-column label="剩余房间数量" prop="leaveRooms" min-width="50"/>-->
      <el-table-column label="操作" min-width="150" align="center">
        <template #default="scope">
          <el-button type="text" @click="addHotelDate(scope.row)" >选择酒店日期</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--添加酒店日期-->
    <dialogAddHotel :props="dialogProps" @submitForm="resetQuery" />

    <pagination
        v-show="total > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        :total="total"
        @pagination="getList"
    />
  </div>
</template>

<script setup>
import {reactive, ref} from 'vue'
import {getCurrentInstance, onMounted} from '@vue/runtime-core'
import { toRefs } from '@vueuse/shared'
import {getAddPersonExpensesList,addExpenses} from '../../../../../api/train/verity'
import {useRouter} from 'vue-router'
import { useRoute } from 'vue-router'
import dialogAddHotel from '../dialog/addHotel'
const route = useRoute()
const router = useRouter()

const tableData = ref([])
const total = ref(0)
const loading = ref(true)
const {proxy} = getCurrentInstance()
const trainId = route.params.trainId
const userId = route.params.userId
let selectedIds = []
const dialogPropsBase = {
  open: false,
  isView: false,
  title: '',
  data: null
}
const dialogProps = reactive({
  ...dialogPropsBase
})
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    trainId: trainId,
    name: undefined,
    flag: 0
  },
  rules: {},
})
const {queryParams, form, rules} = toRefs(data)

// 获取列表
function getList(){
  loading.value = true
  getAddPersonExpensesList(queryParams.value).then(res => {
    loading.value = false
    tableData.value = res.rows
    total.value = res.total
  })
}

function handleQuery(){
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery(){
  proxy.resetForm('queryRef')
  handleQuery()
}

function selectable(row,index){
  if (row.isNecessary == 1){
    return false
  }else {
    return true
  }
}

function addHotelDate(row){
  _.assign(dialogProps, dialogPropsBase,{
    open: true,
    isView:true,
    title: '酒店日期',
    // trainId: trainId,
    // userId: route.params.userId
    data:{...row},
    })
}

function handleSelectionChange(selection) {
  selectedIds = selection.map(item => item.id)
}

//添加项目
function addAccommodation(){
  let param = {
    ids: selectedIds,
    userId: route.params.userId,
    trainId: trainId,
    flag: 0
  }
  var title = `是否选择添加 ${selectedIds.length}个食宿项目？`
  proxy.$modal.confirm(title).then(function (){
    return addExpenses(param)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('添加成功！')
  })
}

onMounted(()=>{
  getList()
})
</script>


<style scoped>

</style>
